import 'package:flutter/cupertino.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:s_mankey/config/net/smonkey_ui_api.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/model/banner_model.dart';
import 'package:s_mankey/model/user_model.dart';
import 'package:s_mankey/utils/audio_playes.dart';
import 'package:s_mankey/view_model/login_vmodel.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';
import 'package:localstorage/localstorage.dart';

///全局信息管理
class StorageManager {
  ///app全局配置 eg: theme 持久化数据, 保持键值对信息
  static SharedPreferences sharedPreferences;

  ///临时目录,保存cookie信息
  static Directory temporaryDirectory;

  ///初始化必备操作 eg:user数据
  static LocalStorage localeStorage;

  /// 用户信息
  static UserModel userModel;

  /// 必备数据的初始化操作
  ///
  /// 由于是同步操作会导致阻塞,所以应尽量减少存储容量
  static init({BuildContext context}) async {
    // async 异步操作
    // sync 同步操作
    temporaryDirectory = await getTemporaryDirectory();
    sharedPreferences = await SharedPreferences.getInstance();
    localeStorage = LocalStorage('LocalStorage');
    cmmPlayer.init(); //初始化音频

    //是否准备好读写
    await localeStorage.ready;

    var data = StorageManager.localeStorage.getItem(AppGlobalConfig.USERINFO);
    if(data != null){
      userModel = UserModel.fromJson(data);
      http.options.headers = {'toekn': userModel.accessToken};
      debugPrint('userModel第一次进入${userModel.accessToken}');
    }
  }
}
